package com.six.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.six.pojo.CaiBook;
import com.six.pojo.Cc;
import com.six.pojo.YuDin;
import com.six.service.CaiService;
import com.six.service.ZuoziService;

@Controller
public class Caicontroller {
	@Autowired
	private CaiService caiService;
	@Autowired
	private ZuoziService zuoziService;
	
	@RequestMapping("caidan/{tid}")
	private ModelAndView selectAllcai(@PathVariable("tid") int tid){
		int i=zuoziService.updatezhuoziyou(tid);
		ModelAndView modelAndView=new ModelAndView("caidan");
		List<CaiBook> caiBooks =caiService.selectAllCai();
		modelAndView.addObject("caiBooks", caiBooks);
		modelAndView.addObject("tid", tid);
		return modelAndView;
	}

	@RequestMapping("tiaojian")
	public @ResponseBody List<CaiBook> selectAlltaiojian( String ftype){
		List<CaiBook> list=caiService.selectAllCaitiaojian(ftype);
		return list;
	}
	@RequestMapping("caidan")
	public @ResponseBody List<CaiBook> selectAllcaidan(int tid,int pid){
		int i=caiService.insertcaidan(tid, pid);
		List<CaiBook> cList=caiService.selectAllcaidan(tid);
		return cList;
	}
	@RequestMapping("tijiao/{zprice}/{tid}")
	public ModelAndView tijiao(HttpSession session,@PathVariable("zprice")int zprice,@PathVariable("tid")int tid){
		ModelAndView modelAndView=new ModelAndView("fuqian");

		List<CaiBook> cailists=caiService.selectcai(tid);
		session.setAttribute("cailists", cailists);
		modelAndView.addObject("tid", tid);
		modelAndView.addObject("zprice", zprice);
		return modelAndView;
		
	}
	@RequestMapping("jiez/{zprice}/{tid}")
	public String jiez(@PathVariable("zprice")int zprice,@PathVariable("tid")int tid){
		ModelAndView modelAndView=new ModelAndView("zuozi");
		caiService.deletecai(tid);
		SimpleDateFormat timeFromate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dtime=new Date();
		String date=timeFromate.format(dtime);
		caiService.insertjiage(zprice, date);
		zuoziService.updatezhuoziwu(tid);
		return "redirect:../../zhuozi";
	}
	@RequestMapping("from/{tid}")
	public ModelAndView from(@PathVariable("tid")int tid){
		ModelAndView modelAndView=new ModelAndView("yuding");
		modelAndView.addObject("tid", tid);
		return modelAndView;
	}
	@RequestMapping("yudin")
	public String yuding(YuDin yuDin){
		int i=caiService.insertdengji(yuDin);
		int u=zuoziService.updatezhuoziyou(yuDin.getTid());
		 return "redirect:zhuozi1";
	}
	@RequestMapping("qiantai")
	public ModelAndView qiantai(){
		ModelAndView modelAndView=new ModelAndView("qiantai");
		 return modelAndView;
	}
	@RequestMapping("rcc")
	public ModelAndView rc(){
		ModelAndView modelAndView=new ModelAndView("rc");
		List<CaiBook> clList=caiService.selectAllCai();
		modelAndView.addObject("clList", clList);
		return modelAndView;
	}
	@RequestMapping("xg/{pid}")
	public ModelAndView  xg(@PathVariable("pid") int pid){
		ModelAndView modelAndView=new ModelAndView("xiugai");
		CaiBook cb=caiService.selectByid(pid);
		modelAndView.addObject("cb", cb);
		modelAndView.addObject("pid", pid);
		return modelAndView;
	}
	@RequestMapping("xgg")
	public String xgg(Cc c){
		int i=caiService.update(c);
		return "redirect:rcc";
	}
	@RequestMapping("like")
	public ModelAndView like(String pname){
		ModelAndView modelAndView=new ModelAndView("rc");
		List<CaiBook> clList=caiService.selectByname(pname);
		modelAndView.addObject("clList", clList);
		return modelAndView;
	}

}
